const modelBusiness = require('camraBnessgai.js')

const { registerGLTFLoader } = require('GLTFLoader.js');

const canvasId = 'canvas1';
// a glb model url
var modelUrl,sitetype
var pointnum,pointnum
// var modelUrl = 'https://www.juquduo.com/static/models/act.glb'
var isDeviceMotion = false;
var isAndroid = false;
// camera listener
var listener = null;
var acid;var openid,pointmsg,randid,name1;

Page({
  data: {
    devicePosition: 'back',
    anima:[],showtishi:true,
    modelone:[],
    bloodstate:100,
    direction:'',success:false,
    xiedu:''
  },
  onLoad(options) {
    var _that = this;
    randid = options.randid
    sitetype = options.sitetype
    pointnum = wx.getStorageSync('pointnum')
    pointmsg = wx.getStorageSync('pointmsg')
    acid = wx.getStorageSync('acidacid')
    openid = wx.getStorageSync('openid')
    console.log('record的参数',openid,'acid',acid,'分支名',pointmsg.BranchName,'randid',randid,'分数',pointmsg.Score)
    // pointnum = placemsg.Pointnum+1,pointnum是主线关卡数
    if(pointnum==undefined||pointnum=='undefined'||sitetype==undefined){
      pointnum=1,sitetype=1
      console.log('设置默认参数')
    }
    // sitetype='tree',sitetype = 1，sitetype为1，为树形点位，为2，为单线点位
    if(sitetype=='1'){
      name1 = 'gbxs'+ 1
      modelUrl = 'https://www.juquduo.com/static/models/gbsz/'+ name1 +'.glb'     //古北水镇
    }
    if(sitetype=='2'){
      name1 = 'ydxs'+ 1
      modelUrl = 'https://www.juquduo.com/static/models/ydjd/'+ name1 +'.glb'    //云顶酒店
    }
    // modelUrl = 'https://www.juquduo.com/static/models/'+ pointnum +'xs.glb'N.glb 
    // modelUrl = 'https://www.juquduo.com/static/models/scc/'+ pointnum +'xs.glb'
    // console.log(placemsg)
    console.log('这是第1个文件，第',pointnum,'个模型')

    // var placemsg = 2
    // modelUrl = 'https://www.juquduo.com/static/models/'+placemsg+'xs.glb'
    // set cameraStyle of camera by system platform
    const res = wx.getSystemInfoSync();
    console.log(res.system);
    if (res.system.indexOf('Android') !== -1) {
      isAndroid = true;
    }
    console.log('modelUrl',modelUrl)
    modelBusiness.initThree(canvasId,name1,modelUrl,
      function (THREE) {
        // console.log(modelUrl)
        modelBusiness.initScene(false);
      });
      // 开始方向检测
      modelBusiness.startDeviceMotion(isAndroid);
      isDeviceMotion = true;
  },
  onUnload() {
    isDeviceMotion = false;
    modelBusiness.stopanimate();
    modelBusiness.stopDeviceMotion();
    this.stopTacking();
    console.log('onUnload', 'listener is stop');
  },
  toctn(){
    var isover = wx.getStorageSync('isover')
    if(isover==true){
      wx.redirectTo({
        url: '../dgameques/dgameover',
      })
    }else{
      wx.redirectTo({
      url: '../dgame1/dgame1',
    })
    }
    
  },
  bindtouchstart_callback(event) {
    modelBusiness.onTouchstart(event);
  },
  bindtouchmove_callback(event) {
    modelBusiness.onTouchmove(event);
  },
  startTacking() {
    var _that = this;
    const context = wx.createCameraContext();

    if (!context.onCameraFrame) {
      var message = 'Does not support the new api "Camera.onCameraFrame".';
      console.log(message);
      wx.showToast({
        title: message,
        icon: 'none'
      });
      return;
    }

    // real-time
    listener = context.onCameraFrame(async function (res) {
      console.log('onCameraFrame:', res.width, res.height);
      const cameraFrame = {
        data: new Uint8Array(res.data),
        width: res.width,
        height: res.height,
      };
      modelBusiness.setCameraFrame(cameraFrame);
    });
    // start
    listener.start();
    console.log('startTacking', 'listener is start');
  },
  start(){
    console.log('modelUrl',modelUrl)
    modelBusiness.initThree(canvasId,modelUrl,
      function (THREE) {
        // console.log(THREE)
        modelBusiness.initScene(false);
      });
  },
  stopTacking() {
    if (listener) {
      listener.stop();
    }
  },
  changeDirection() {
    var status = this.data.devicePosition;
    if (status === 'back') {
      status = 'front';
    } else {
      status = 'back';
    }
    this.setData({
      devicePosition: status,
    });
  },
  //另一个文件里的代码，检测镜头方向、添加交互------------------
  toggleDeviceMotion() {
    if (isDeviceMotion) {
      modelBusiness.stopDeviceMotion();
    } else {
      modelBusiness.startDeviceMotion(isAndroid);
    }
    isDeviceMotion = !isDeviceMotion;
  },
  touchStart(e) {
    // this.canvas.dispatchTouchEvent({...e, type:'touchstart'})
  },
  touchMove(e) {
    // this.canvas.dispatchTouchEvent({...e, type:'touchmove'})
  },
  touchEnd(e) {
    // this.canvas.dispatchTouchEvent({...e, type:'touchend'})
  },
  //-----------------------------------------------------------
  showbtn(){
    this.setData({success:true})
  },
  hidetishi(){
    this.setData({showtishi:false})
  },
  caozuo1(){
    var blood = this.data.bloodstate
    blood = blood-5
    this.setData({bloodstate:blood})
    
  }
  
});